import Vue from 'vue'
import vuex from 'vuex'
import axios from 'axios'
import url from '../js/config'
import router from '../router'

Vue.use(vuex);


export default new vuex.Store({
    state:{
        username:"admin",
        usermoney:"1000",
        show:false,
        token:'',
        gameType:'两面盘',
    },
    mutations:{
        changeGame(state,text){
            state.gameType=text
        },
        changeToken(state,text){
            state.token=text
        }
    },
    actions:{
        login (context,user) {
            axios({
              method: 'post',
              url: url.login,
              data: user,
            }).then(res => {
                console.log(1)
                res.data.code!==0?router.replace({
                    path: '/poo',
                }):router.replace({
                    path: '/poo',
                })
            }).catch(
                err => {
                    err.code!==0?router.replace({
                        path: '/poo',
                    }):router.replace({
                        path: '/poo',
                    })
                }
            )
        },
        getDate(context){
            axios({
                method: 'get',
                url: url.getDate,
            }).then(res => {
                context.commit('changeToken',res.data.token)
            })
        }

    }
})